package net.easyjava.web.constants;

import com.alibaba.fastjson.JSONObject;
import net.easyjava.tools.BaseEnum;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wxnacy on 16/1/1.
 */
public enum  FocusConstants implements BaseEnum {
    FOCUS_MONGODB(2,1,"MongoDB",1,"",1),
    FOCUS_MYSQL(3,1,"Mysql",1,"",2),
    FOCUS_TECHNOLOGY(1,0,"技术",3,"/technology.html",1),
    FOCUS_LIANXI(1000,0,"联系",3,"/connection.html",2),

    ;
    private FocusConstants(int id, int fatherId, String name, int type,String url,int displayOrder) {
        this.id = id;
        this.fatherId = fatherId;
        this.name = name;
        this.type = type;
        this.url = url;
        this.displayOrder = displayOrder;
    }

    private int id;
    private int fatherId;
    private String name;
    private int type;//1:文章列表、2：文章详情、3：链接地址
    private String url;
    private int displayOrder;
    private List subFocuss;

    public int getId() {
        return id;
    }

    public int getFatherId() {
        return fatherId;
    }

    public String getName() {
        return name;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public List getSubFocuss() {
        return subFocuss;
    }

    public void setSubFocuss(List subFocuss) {
        this.subFocuss = subFocuss;
    }

    public int getDisplayOrder() {
        return displayOrder;
    }


    /**
     * 通过url获取
     * @param url
     * @return
     */
    public static FocusConstants getObjectByUrl(String url){
        FocusConstants[] sets =  FocusConstants.values();
        for (FocusConstants object:sets){
            if(object.getUrl().equals(url)){
                return object;
            }
        }
        return null;
    }

    /**
     * 获取级联菜单
     * @param fatherId
     * @return
     */
    public static List<FocusConstants> getList(int fatherId){
        FocusConstants[] sets =  FocusConstants.values();
        List<FocusConstants> list = new ArrayList<FocusConstants>();
        for (FocusConstants object:sets){
            if(object.getFatherId() ==fatherId){
                if(fatherId ==0){
                    //获取子菜单
                    object.setSubFocuss(getList(object.getId()));
                }
                //添加
                list.add(object);
            }
        }
        return list;
    }
    public static List getJsonList(int fatherId){
        FocusConstants[] sets =  FocusConstants.values();
        List list = new ArrayList();
        for (FocusConstants object:sets){
            if(object.getFatherId() ==fatherId){
                if(fatherId ==0){
                    //获取子菜单
                    object.setSubFocuss(getJsonList(object.getId()));
                }
                //添加
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("id",object.getId());
                jsonObject.put("fatherId",object.getFatherId());
                jsonObject.put("name",object.getName());
                jsonObject.put("type",object.getType());
                jsonObject.put("url",object.getUrl());
                jsonObject.put("displayOrder",object.getDisplayOrder());
                list.add(jsonObject);
            }
        }
        return list;
    }

    @Override
    public String toString() {
        try {
            return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
        } catch (Exception e) {
            return super.toString();
        }
    }

    public static void main(String[] args) {
        System.out.println("FocusConstants.getList(0) = " + FocusConstants.getList(0));

    }
}
